pub mod fs;

pub mod parse;

mod prelude {
    use yaml_rust::{Yaml, YamlLoader};

    use crate::fs::{gen_toml, read_lines, read_yaml};

    use std::{fs::File, io::Write, path::Path};

    pub fn yaml2toml<P, F>(from: P, to: P, parse: F) -> anyhow::Result<()>
    where
        P: AsRef<Path>,
        F: FnOnce(Yaml) -> anyhow::Result<toml::Value>,
    {
        let lines = read_lines(from)?;
        let (x, y, s) = read_yaml(&lines)?;
        let docs = YamlLoader::load_from_str(&s)?;
        let output = gen_toml(lines, (x, y, toml::to_string(&parse(docs[0].clone())?)?));

        let mut file = File::create(to)?;
        file.write_all(output.join("\r\n").as_bytes())?;
        Ok(())
    }
}

pub use prelude::*;
